﻿package com.examplee.username.mylock;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Switch;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by usernamee on 2017/6/12.
 */

public class LockPatternView extends View {
//至少有5个点
    private static int POINT_SIZE=5;
//    矩阵
    private Matrix matrix=new Matrix();
    private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
   private Point[][] points=new Point[3][3];
     boolean isInit,isSelect,isFinsh,movingNoPoint;
 private    float width;
  private   float height;
  private   float offsetsX,offsetsY,bitmapR,movingX,movingY;
    private Bitmap pointince1,pointince2,pointince3,pointline1,pointline2;
    private List<Point> pointList=new ArrayList<>();
    public LockPatternView(Context context) {
        super(context);
    }

    public LockPatternView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        初始化

        if (!isInit){
            initPoints();
        }
//        画点
        points2Canvas(canvas);
//        画线
        if (pointList.size()>0){
            Point a=pointList.get(0);
            for (int i=0;i<pointList.size();i++){
                Point b=pointList.get(i);
                line2Canvas(canvas,a,b);
                a=b;
            }
            if (movingNoPoint){
                line2Canvas(canvas,a,new Point(movingX,movingY));
            }
        }
    }
//将点绘制到画布
    private void points2Canvas(Canvas canvas) {
for (int i=0;i<points.length;i++){
    for (int j=0;j<points[i].length;j++){
        Point point=points[i][j];
        if (point.state==Point.STATE_PERSSED){
            canvas.drawBitmap(pointince3,point.x-bitmapR,point.y,paint);
        }else if (point.state==Point.STATE_ERROR){
            canvas.drawBitmap(pointince1,point.x-bitmapR,point.y,paint);
        }else{
            canvas.drawBitmap(pointince2,point.x-bitmapR,point.y,paint);

        }
    }
}
    }
//    画线
private void line2Canvas(Canvas canvas,Point a,Point b){
    float linelength=(float) Point.distance(a,b);
    float degrees= getDegrees(a,b);
    if (a.state==Point.STATE_PERSSED){
        canvas.rotate(degrees,a.x,a.y);
        matrix.setScale(linelength/pointline1.getWidth(),1);
        matrix.postTranslate(a.x-pointline1.getWidth()/2,a.y-pointline1.getHeight()/2);
        canvas.drawBitmap(pointline1,matrix,paint);
    }else{
        matrix.setScale(linelength/pointline2.getWidth(),1);
        matrix.postTranslate(a.x-pointline2.getWidth()/2,a.y-pointline2.getHeight()/2);
        canvas.drawBitmap(pointline2,matrix,paint);
    }
    canvas.rotate(-degrees,a.x,a.y);
}

    private float getDegrees(Point a, Point b) {
        float ax=a.x;
        float ay=a.y;
        float bx=b.x;
        float by=b.y;
        float degrees=0;
        if (bx==ax){//x轴相等 90度或270度
            if (by>ay){//在y轴的下边 90度
                degrees=90;
            }else if (by<ay){//在y轴的上边270度
                degrees=270;
            }else if (by==ay){//y轴相等 0度或180度
                if (bx>ax){
                    degrees=0;
                }else if (bx<ax){
                    degrees=180;
                }
            }
        }
        return degrees;
    }

    //初始化点
    private void initPoints() {
//       1. 获取布局的宽高
        width=getWidth();
        height=getHeight();
//      2. 偏移量
//        横屏
        if (width>height){
            offsetsX=(width-height)/2;
            width=height;
        }
//       竖屏
        else {
            offsetsY=(height-width)/2;
            height=width;
        }
//       3. 图片资源
        pointince1=BitmapFactory.decodeResource(getResources(),R.drawable.ince1);
        pointince2= BitmapFactory.decodeResource(getResources(),R.drawable.ince3);
        pointince3= BitmapFactory.decodeResource(getResources(),R.drawable.ince2);

        pointline1= BitmapFactory.decodeResource(getResources(),R.drawable.line1);
        pointline1=BitmapFactory.decodeResource(getResources(),R.drawable.line2);
//        4.点的坐标
        points[0][0]=new Point(offsetsX+width/4,offsetsY+width/4);
        points[0][1]=new Point(offsetsX+width/2,offsetsY+width/4);
        points[0][2]=new Point(offsetsX+width-width/4,offsetsY+width/4);

        points[1][0]=new Point(offsetsX+width/4,offsetsY+width/2);
        points[1][1]=new Point(offsetsX+width/2,offsetsY+width/2);
        points[1][2]=new Point(offsetsX+width-width/4,offsetsY+width/2);

        points[2][0]=new Point(offsetsX+width/4,offsetsY+width-width/4);
        points[2][1]=new Point(offsetsX+width/2,offsetsY+width-width/4);
        points[2][2]=new Point(offsetsX+width-width/4,offsetsY+width-width/4);
//        5.图片资源的半径
        bitmapR=pointince1.getHeight()/2;
//        6.初始化完成
        isInit=true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        movingNoPoint=false;
        movingY=event.getY();
        movingX= event.getX();
        Point point=null;
        switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:
                resetPoint();
                point=checkSelectPoint();
                if (point!=null){
                    isSelect=true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isSelect){
                    point=checkSelectPoint();
                    if (point==null){
                        movingNoPoint=true;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                isSelect=false;
                isFinsh=true;
                break;

        }
//        选中重复检查
        if (!isFinsh&&isSelect&&point!=null){
//            交叉点
if (crossPoint(point)){
    movingNoPoint=true;
}
//新点
else{
    point.state=Point.STATE_PERSSED;
    pointList.add(point);
}
        }
//            绘制结束
        if (isFinsh){
//            绘制不成立
if (pointList.size()==1){
    resetPoint();
}
//绘制错误
else if (pointList.size()<POINT_SIZE&&pointList.size()>2){
    errorPoint();
}
        }
//        刷新View
        postInvalidate();
        return true;
    }
    public boolean crossPoint(Point point){
        if (pointList.contains(point)){
            return true;
        }
        else {
            return false;
        }
    }
    public void resetPoint(){
        pointList.clear();
    }
//    设置绘制错误
    public void errorPoint(){
        for (Point point:pointList){
            point.state=Point.STATE_ERROR;

        }
    }
//    检查是否选中
private Point checkSelectPoint(){
    for (int i=0;i<points.length;i++){
        for (int j=0;j<points[i].length;j++){
            Point point=points[i][j];
            if (Point.with(point.x,point.y,bitmapR,movingX,movingY)){
                return point;
            }

        }
    }
    return null;
}
    public LockPatternView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /*
    * 自定义的点
    */
    public static class Point{
// 正常
  public static int STATE_NORMAL=0;
//  选中
public static int STATE_PERSSED=1;
// 错误
public static int STATE_ERROR=2;
        public float x,y;
        public int index=0,state=0;
        public Point(){

        }
        public Point(float x,float y){
            this.x=x;
            this.y=y;
        }
        public static double distance(Point a,Point b){
            return Math.sqrt(Math.abs(a.x-b.x)*Math.abs(a.x-b.x)+Math.abs(a.y-b.y)*Math.abs(a.y-b.y));
        }
        /*
        是否重合
         */
        public static boolean with(float pointX,float pointY,float r,float movingX,float movingY){
//       开方
            return Math.sqrt((pointX-movingX)*(pointX-movingX)+(pointY-movingY)*(pointY-movingY))<r;
        }
    }

}
